package com.ktjiaoyu.demo;

import com.ktjiaoyu.demo.entity.User;
import com.ktjiaoyu.demo.repository.UserRepository;
import com.ktjiaoyu.demo.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTset {
    @Resource
    private UserRepository userRepository;
    @Resource
    private UserService userService;

    @Test
    public void test(){
        System.out.println(123);
        User user = new User("JPA","123456",4L,2);
        userRepository.save(user);
    }

    @Test
    public void testGet(){
        User user = userRepository.findById(2L).get();
        System.out.println("username"+user.getUsrName());
    }

    @Test
    public void testFindByName(){
        List<User> list = userRepository.findByUsrName("hu");
        System.out.println(list);
    }

    @Test
    public void testFindByNameLike(){
        List<User> list = userRepository.findByUsrNameLike("%u%");
        if (list!=null){
            for (User user : list){
                System.out.println(user.getUsrName());
            }
        }
    }

    @Test
    public void findByRoleId(){
        List<User> list = userRepository.findByRoleId(4L);
        if (list!=null){
            for (User user : list){
                System.out.println(user.getUsrName());
            }
        }
    }

    @Test
    public void modifyNameById(){
        int ret = userRepository.modifyNameById("123",1L);
        System.out.println("数量:"+ret);
    }

    @Test
    public void findUsersByName(){
        List<User> list = userRepository.findUsersByName("hu");
        System.out.println(list);
    }

    @Test
    public void findPageByUsrRoleId(){
        int page = 0,sezi = 2;
        Sort sort = Sort.by(Sort.Direction.DESC,"usrId");
        Pageable pageable = PageRequest.of(page,sezi,sort);
        Page<User> userPage = userRepository.findPageByUsrRoleId(2L,pageable);

        System.out.println("总记录数:" + userPage.getTotalElements());
        System.out.println("总页数:" + userPage.getTotalPages());
        System.out.println("当前页数:" + (pageable.getPageNumber())+1);
        System.out.println("每页记录数:" + userPage.getSize());
        System.out.println("当前页记录数:" + userPage.getNumberOfElements());
        for (User user : userPage.getContent()){
            System.out.println("usrId:"+user.getUsrId());
        }
    }

    @Test
    public void findPageByMap(){
        int page = 0,size = 2;
        Sort sort = Sort.by(Sort.Direction.DESC,"usrId");
        Pageable pageable = PageRequest.of(page,size,sort);
        Map param = new HashMap();
        param.put("roleId",2L);
        Page<User> userPage = userService.findPageByMap(param,pageable);

        System.out.println("总记录数:"+userPage.getTotalElements());
        System.out.println("总页数:"+userPage.getTotalPages());
        System.out.println("当前页数:"+(pageable.getPageNumber())+1);
        System.out.println("每页记录数:"+userPage.getSize());
        System.out.println("当前页记录数:"+userPage.getNumberOfElements());
        for (User user : userPage.getContent()){//获得查询记录
            System.out.println("usrId:"+user.getUsrId());
        }
    }
}
